package cn.itcast.nems.employee.web.contorller.b;

import cn.itcast.ic.common.bean.Page;
import cn.itcast.nems.common.PageQueryModel;
import cn.itcast.nems.employee.bo.AddEmployeeToGroupBo;
import cn.itcast.nems.employee.bo.DeleteEmployeeFromGroupBo;
import cn.itcast.nems.employee.bo.EmployeeGroupCreateBo;
import cn.itcast.nems.employee.bo.EmployeeGroupUpdateBo;
import cn.itcast.nems.employee.dto.*;
import cn.itcast.nems.employee.service.EmployeeGroupService;
import groovy.util.logging.Slf4j;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/b/employee/group")
@Slf4j
@RequiredArgsConstructor
public class EmployeeGroupController {

    private final EmployeeGroupService employeeGroupService;


    // 查找
    @PostMapping("/page/list")
    public ResponseEntity<Page<EmployeeGroupDto>> findEmployeeGroupPageList(@RequestBody PageQueryModel<EmployeeGroupQueryDto> queryModel) {
        return ResponseEntity.ok(employeeGroupService.findEmployeeGroupPageList(queryModel));
    }

    // 添加
    @PostMapping
    public ResponseEntity<Boolean> createEmployeeGroup(@RequestBody EmployeeGroupCreateBo bo) {
        return ResponseEntity.ok(employeeGroupService.createEmployeeGroup(bo));
    }

    // 编辑
    @PutMapping("/{id}")
    public ResponseEntity<Boolean> updateEmployeeGroup(@RequestBody EmployeeGroupUpdateBo bo, @PathVariable String id) {
        bo.setId(id);
        return ResponseEntity.ok(employeeGroupService.updateEmployeeGroup(bo));
    }

    // 删除
    @DeleteMapping("/{id}")
    public ResponseEntity<Boolean> deleteEmployeeGroup(@PathVariable String id) {
        return ResponseEntity.ok(employeeGroupService.deleteEmployeeGroup(id));
    }

    // 用户按钮（用户组对应的用户列表）
    @PostMapping("/{id}/members/page/list")
    public ResponseEntity<Page<EmployeeInGroupDto>> findEmployeeFromGroupPageList(@RequestBody PageQueryModel<EmployeeFromGroupQueryDto> queryModel, @PathVariable String id) {
        queryModel.getCondition().setGroupId(id);
        return ResponseEntity.ok(employeeGroupService.findEmployeeFromGroupPageList(queryModel));
    }

    // 用户窗口的添加按钮
    @PostMapping("/{id}/members")
    public ResponseEntity<Boolean> addEmployeeToGroup(@RequestBody AddEmployeeToGroupBo bo, @PathVariable String id) {
        bo.setGroupId(id);
        return ResponseEntity.ok(employeeGroupService.addEmployeeToGroup(bo));
    }

    // 用户窗口的删除按钮
    @DeleteMapping("/{id}/members")
    public ResponseEntity<Boolean> deleteEmployeeFromGroup(@RequestBody DeleteEmployeeFromGroupBo bo, @PathVariable String id) {
        bo.setGroupId(id);
        return ResponseEntity.ok(employeeGroupService.deleteEmployeeFromGroup(bo));
    }

    // 交互，根据邮箱查找用户
    @GetMapping("/preview/{email}")
    public ResponseEntity<EmployeeInfoDto> employeeAccountInfo(@PathVariable String email) {
        return ResponseEntity.ok(employeeGroupService.employeeAccountInfo(email));
    }

}
